package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.AdminInviteActivityRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.AdminInviteActivityMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.AdminInviteActivity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IAdminInviteActivityService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】业务层处理
 *
 * @author wang
 * @date 2024-07-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminInviteActivityServiceImpl implements IAdminInviteActivityService {

    private final AdminInviteActivityMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminInviteActivity add(AdminInviteActivityRequest request) {
        AdminInviteActivity entity = AdminInviteActivity.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminInviteActivityRequest request) {
        AdminInviteActivity entity = AdminInviteActivity.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminInviteActivity getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminInviteActivity> pageList(AdminInviteActivityRequest request) {
        Page<AdminInviteActivity> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminInviteActivity> query = this.buildWrapper(request);
        IPage<AdminInviteActivity> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminInviteActivity> list(AdminInviteActivityRequest request) {
        LambdaQueryWrapper<AdminInviteActivity> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminInviteActivity> buildWrapper(AdminInviteActivityRequest param) {
        LambdaQueryWrapper<AdminInviteActivity> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getUserId())){
               query.like(AdminInviteActivity::getUserId ,param.getUserId());
        }
        if (param.getPrizeId() != null){
               query.eq(AdminInviteActivity::getPrizeId ,param.getPrizeId());
        }
        if (param.getReplayId() != null){
               query.eq(AdminInviteActivity::getReplayId ,param.getReplayId());
        }
         if (StringUtils.isNotBlank(param.getStartTime())){
               query.like(AdminInviteActivity::getStartTime ,param.getStartTime());
        }
         if (StringUtils.isNotBlank(param.getEndTime())){
               query.like(AdminInviteActivity::getEndTime ,param.getEndTime());
        }
        if (param.getIsGet() != null){
               query.eq(AdminInviteActivity::getIsGet ,param.getIsGet());
        }
         if (StringUtils.isNotBlank(param.getCreateUserId())){
               query.like(AdminInviteActivity::getCreateUserId ,param.getCreateUserId());
        }
         if (StringUtils.isNotBlank(param.getUpdateUserId())){
               query.like(AdminInviteActivity::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
